<template>
  <div class="app-container">
    <div class="head-container flex-center" style="border-bottom: 1px solid #ddd;">
      <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>文章管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div style="text-align: right">
        <el-button type="primary"
                   plain
                   size="small"
                   v-has-permi="['system:article:insert']"
                   @click="handleAdd">新增文章
        </el-button>
        <el-button type="danger"
                   plain
                   size="small"
                   :disabled="multiple"
                   v-has-permi="['system:article:delete']"
                   @click="handleDelete">删除文章
        </el-button>
      </div>
    </div>
    <div class="body-container">
      <el-form :model="queryParams"
               ref="queryForm"
               class="queryForm"
               :inline="true">
        <el-form-item label="文章标题：">
          <el-input v-model="queryParams.title"
                    placeholder="请输入文章标题"
                    clearable
                    size="small"
                    @input="getList"/>
        </el-form-item>
        <el-form-item label="文章类型：">
          <Select v-model="queryParams.type"
                  placeholder="请选择文章类型"
                  size="small"
                  :options="typeOptions"
                  @change="val => selectQuery(val, 'type')"/>
        </el-form-item>
        <el-form-item label="审核状态：">
          <Select v-model="queryParams.flow"
                  placeholder="请选择审核状态"
                  size="small"
                  :options="flowOptions"
                  @change="val => selectQuery(val, 'flow')"/>
        </el-form-item>
        <el-form-item label="文章状态：">
          <Select v-model="queryParams.status"
                  placeholder="请选择文章状态"
                  size="small"
                  :options="statusOptions"
                  @change="val => selectQuery(val, 'status')"/>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading"
                :data="articleList"
                border
                @selection-change="handleSelectionChange">
        <el-table-column type="selection"
                         width="55" />
        <el-table-column label="文章标题"
                         prop="title"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="文章类型"
                         prop="type"
                         width="120"
                         :formatter="row => selectDictLabel(typeOptions, row.type)"/>
        <el-table-column label="文章状态"
                         prop="status"
                         width="120"
                         :formatter="row => selectDictLabel(statusOptions, row.status)"/>
        <el-table-column label="发布者"
                         width="120"
                         prop="author.name"/>
        <el-table-column label="审核状态"
                         width="120"
                         prop="flow"
                         :formatter="row => selectDictLabel(flowOptions, row.flow)" />
        <el-table-column label="创建时间"
                         width="220"
                         prop="createTime"
                         :formatter="row => dateFormatter(row)"/>
        <el-table-column label="操作"
                         width="220">
          <template slot-scope="scope">
            <el-button size="small"
                       type="primary"
                       plain
                       v-has-permi="['system:article:update']"
                       @click="handleUpdate(scope.row)">修改
            </el-button>
            <el-button size="small"
                       type="danger"
                       plain
                       v-has-permi="['system:article:delete']"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改文章对话框 -->
    <el-dialog :title="title"
               :visible.sync="open"
               width="90%"
               :close-on-click-modal="false"
               append-to-body>
      <el-form ref="form"
               :model="form"
               :rules="rules"
               label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="文章标题：" prop="title">
              <el-input v-model="form.title" placeholder="请输入文章标题"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文章类型：" prop="type">
              <Select v-model="form.type"
                      :options="typeOptions"
                      placeholder="请选择文章类型"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文章摘要：" prop="abstract">
              <el-input v-model="form.abstract" placeholder="请输入文章摘要"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文章状态：">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in statusOptions"
                          :key="dict.dictValue"
                          :label="dict.dictValue">{{dict.dictLabel}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容：" prop="content">
              <editor v-model="form.content" :min-height="450"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listArticleAll, updateArticle, delArticle, addArticle, getArticle} from "@/api/system/article"

  export default {
    name: "Notice",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 文章表格数据
        articleList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 类型数据字典
        statusOptions: [],
        // 状态数据字典
        typeOptions: [],
        // 审核状态字典
        flowOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: undefined,
          type: undefined,
          flow: undefined,
          status: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          title: [
            {required: true, message: "文章标题不能为空", trigger: "blur"}
          ],
          type: [
            {required: true, message: "文章类型不能为空", trigger: "change"}
          ]
        }
      }
    },
    created() {
      this.getList()
      this.getDicts("sys_article_status").then(res => {
        this.statusOptions = res.data
      })
      this.getDicts("sys_article_type").then(res => {
        this.typeOptions = res.data
      })
      this.getDicts('sys_flow_status').then(res => {
        this.flowOptions = res.data
      })
    },
    methods: {
      /** 查询文章列表 */
      getList() {
        this.loading = true
        listArticleAll(this.queryParams).then(response => {
          this.articleList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          _id: undefined,
          title: undefined,
          type: undefined,
          content: undefined,
          abstract: undefined,
          status: "1"
        }
        this.resetForm("form")
      },
      /** 重置按钮操作 */
      selectQuery(val, key) {
        if (val) this.queryParams[key] = val
        else delete this.queryParams[key]
        this.getList()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item._id)
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = "新增文章"
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        getArticle(row._id).then(response => {
          this.form = response.data
          console.log(this.form);
          this.open = true
          this.title = "修改文章"
        })
      },
      /** 提交按钮 */
      submitForm () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form._id !== undefined) {
              updateArticle(this.form).then(response => {
                this.msgSuccess("修改成功")
                this.open = false
                this.getList()
              })
            } else {
              addArticle(this.form).then(response => {
                this.msgSuccess("新增成功")
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const articleIds = row._id ? [row._id] : this.ids
        this.$confirm('是否确认删除所有已选择的文章数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delArticle(articleIds)
        }).then(() => {
          this.getList()
          this.msgSuccess("删除成功")
        }).catch(() => {
        })
      }
    }
  }
</script>
